package com.example.demo.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.example.demo.dto.MenuDto;
import com.example.demo.po.User;
import com.example.demo.service.MenuService;
import com.example.demo.service.UserService;
import com.example.demo.utils.MD5Utils;
import com.example.demo.utils.UUIDUtils;

@Controller
@RequestMapping("/admin")
public class AdminIndexController {
	
	@Autowired
	private MenuService menuService;
	@Autowired
	private UserService userService;
	
	@RequestMapping("/admin.action")
	public String index(Model model,User user){

		
		List<MenuDto> menuDtos = menuService.getAllMenu();
		model.addAttribute("menuDtos", menuDtos);
		return "admin/index";
		
	}
	
	
	/**
	 * 注册
	 * 
	 * @param user
	 * @return
	 */
	@RequestMapping(value = "/registerUser")
	public String register(User user) {
		if (user.getId() != null) {
			userService.loginUser(user);
		} else {
			String pwd=user.getPassword();
			String password=MD5Utils.getMD5(pwd);
			user.setPassword(password);
			user.setStatus(0);
			String code = UUIDUtils.getUUID() + UUIDUtils.getUUID();
			user.setCode(code);
			userService.register(user);
		}

		return "admin/login/success";
	}

	@RequestMapping(value = "/toregisterUser", method = RequestMethod.GET)
	public String toRegisterUser(Model model, Integer id) {

		if (id != null) {

			User user = userService.selectById(id);

			model.addAttribute("user", user);
		}

		return "admin/login/register";
	}

	/**
	 * 校验邮箱中的code激活账户 首先根据激活码code查询用户，之后再把状态修改为"1"
	 */
	@RequestMapping(value = "/checkCode")
	public String checkCode(String code) {
		User user = userService.checkCode(code);
		System.out.println(user);
		// 如果用户不等于null，把用户状态修改status=1
		if (user != null) {
			String pwd=user.getPassword();
			String password=MD5Utils.getMD5(pwd);
			user.setPassword(password);
			user.setStatus(1);
			// 把code验证码清空，已经不需要了
			user.setCode("");
		
			System.out.println(user);
			userService.updateUserStatus(user);
		}
		return "redirect:loginPage";
	}

	/**
	 * 跳转到登录页面
	 * 
	 * @return login
	 */
	@RequestMapping(value = "/loginPage")
	public String login(HttpSession session) {
		if(session.getAttribute("admin")!=null&&session.getAttribute("admin")=="true") {
			
			return "redirect:admin.action";
		}
		return "admin/login/login";
	}

	/**
	 * 登录
	 */
	@RequestMapping(value = "/loginUser")
	public String login(HttpSession session,User user, Model model,HttpServletRequest request) {
		
		String pwd=user.getPassword();
		String password=MD5Utils.getMD5(pwd);
		user.setPassword(password);
		user = userService.loginUser(user);
		if (user != null) {
			session = request.getSession();
			session.setAttribute("admin", user);
			return "redirect:admin.action";
		}
		return "admin/login/fail";

	}

	
	/**
	 * 验证邮箱是否存在
	 */
	@ResponseBody
    @RequestMapping(value = "/verifyEmail", method = RequestMethod.GET)
    public User verifyUser(@RequestParam("email") String email){
        return userService.selectByEmail(email);
    }

}
